//
//  PopupVC.swift
//  PopupDemo
//
//  Created by zhuxuhong on 2020/11/1.
//

import UIKit

open class PopupVC: PannelVC {
    open override func setupWrapperView(_ wrapperView: UIView) {
        updateWrapperView(wrapperView)
        wrapperView.transform = .init(scaleX: 1.05, y: 1.05)
        wrapperView.alpha = 0
    }
    open override func updateWrapperView(_ wrapperView: UIView) {
        let x = (view.bounds.width - panelSize.width) / 2
        let y = (view.bounds.height - panelSize.height) / 2
        wrapperView.frame = .init(origin: .init(x: x, y: y), size: panelSize)
    }
    
    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func animate(
        in isIn: Bool = true,
        for wrapperView: UIView,
        completion: (() -> Void)? = nil
    ) {
        let duration = isIn ? 0.25 : 0.2
        let scale: CGFloat = isIn ? 1.0 : 1.05
        let alpha: CGFloat = isIn ? 1 : 0
        UIView.animate(
            withDuration: duration,
            delay: 0,
            options: [.curveEaseOut]
        ) {
            self.view.alpha = alpha
            wrapperView.transform = .init(scaleX: scale, y: scale)
            wrapperView.alpha = alpha
        } completion: { [weak self] in
            guard $0, let self = self else { return }
            completion?()
            self.isShowing = true
        }
    }
}
